<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-23 10:53:48
 * @Last Modified time: 2023-02-23 11:36:23
 */
namespace app\common\services\statistics;


use frank\base\BaseService;
use frank\exception\CommonException;
use frank\utils\helper\StringHelper;
use frank\utils\helper\ArrayHelper;
use app\common\dao\store\OrderDao;

/**
 * Class StatisticsOrderService
 */
class StatisticsOrderService extends BaseService
{
 	/**
     * StatisticsOrderService constructor.
     * @param OrderDao $dao
     */
    public function __construct(OrderDao $dao)
    {
        $this->dao = $dao;
    }


    /**
     * 获取订单总量
     */
    public function getOrderTotal($day = null)
    {
        return number_format($this->getPayOrderTotal($day, $day));
    }

    /**
     * 获取已付款订单总数 (可指定某天)
     */
    public function getPayOrderTotal($startDate = null, $endDate = null)
    {
        $filter['pay_status'] = 1;
        $filter['order_status'] = [1,2,4];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [strtotime($startDate),strtotime($endDate) + 86400 ];
        }
        return $this->orderTotal($filter);
    }

    /**
     * 获取订单总数量
     */
    public function orderTotal($filter = [], $handle = false)
    {
        if($handle){
            $filter = [];
            // 数据类型
            switch ($handle) {
                case 'delivery':
                    $filter['pay_status'] = 1;
                    $filter['delivery_status'] = 2;
                    $filter['order_status'] = [2,4];
                    break;
                case 'pay':
                    $filter['pay_status'] = 0;
                    $filter['order_status'] = 2;
                    break;
                case 'refund':
                    $filter['refund_status'] = [1,2,3];
                    break;
            }
            return $this->dao->search($filter)->count();
        }else{
            return $this->dao->search($filter)->count();
        }
    }


    /**
     * 获取某天的总销售额
     */
    public function getOrderTotalPrice($day = null)
    {
        return StringHelper::number2($this->orderTotalPrice($day, $day));
    }

    /**
     * 获取某天的总销售额
     */
    public function orderTotalPrice($startDate = null, $endDate = null)
    {
        $filter['pay_status'] = 1;
        $filter['order_status'] = [1,2,4];
        $filter = [];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [strtotime($startDate),strtotime($endDate)+ 86400];
        }
        return $this->dao->search($filter)->sum('pay_price');
    }

    
    /**
     * 获取需要立即处理的交易订单
     */
    public function getOrderTotalByHandle($dataType)
    {
        return number_format($this->orderTotal(null,$dataType));
    }
}
